package edu.buffalo.son.garman;

import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import edu.buffalo.son.garman.entity.ClaimsRecord;
import edu.buffalo.son.garman.entity.PersonCounts;
import edu.buffalo.son.garman.entity.PersonCounts.IndexAlgorithm;

public class ClaimsRecordsData {
	private LinkedHashMap<String, PersonCounts> data = 
	            new LinkedHashMap<String, PersonCounts>();
	
	public void addRecord(ClaimsRecord record) {
	    //TODO lookup for icd9 -> shorthand representation.
	    PersonCounts personData = data.get(record.getPersonId());
	    if(personData == null) {
	        personData = new PersonCounts(record.getPersonId());
	    }
	    
	    if(record.getDx1() != null && !record.getDx1().equals("")) {
	        String category = ICD9ToCategory.lookupCategory(record.getDx1());
	        personData.updateDiseaseCounts(category);
	    }
	    if(record.getDx2() != null && !record.getDx2().equals("")) {
	        String category = ICD9ToCategory.lookupCategory(record.getDx2());
	        personData.updateDiseaseCounts(category);
	    }
	    if(record.getDx3() != null && !record.getDx3().equals("")) {
	        String category = ICD9ToCategory.lookupCategory(record.getDx3());
	        personData.updateDiseaseCounts(category);
	    }
	    if(record.getDx4() != null && !record.getDx4().equals("")) {
	        String category = ICD9ToCategory.lookupCategory(record.getDx4());
	        personData.updateDiseaseCounts(category);
	    }
	    if(record.getDx5() != null && !record.getDx5().equals("")) {
	        String category = ICD9ToCategory.lookupCategory(record.getDx5());
	        personData.updateDiseaseCounts(category);
	    }
	    
	    if(record.getClaimType()  != null) {
            personData.updateClaimCounts(record.getClaimType().toString());
        }
	    
	    data.put(record.getPersonId(), personData);
	}
	
	/*private void updateCount(HashMap<String,Integer> personData, String category) {
	    Integer count = personData.get(category);
        if(count == null) {
            count = 0;
        }
        personData.put(category, count + 1);
 	}*/
		
	public HashMap<String,Integer> getCategoryDistributions(IndexAlgorithm algorithm) {
	    HashMap<String, Integer> categoryCounts = new HashMap<String, Integer>();
	    for(String person:data.keySet()) {
	        PersonCounts personData = data.get(person);
            String category = personData.getCategory(algorithm);
	        Integer count = categoryCounts.get(category);
	        if(count == null) {
	            count = 0;
	        }
	        categoryCounts.put(category, count + 1);
	    }
	    return categoryCounts;
	}
	
	public HashMap<String, String> getPersonCategoryMap(IndexAlgorithm algorithm) {
	    LinkedHashMap<String, String> personCategoryMap = new LinkedHashMap<String, String>();
	    for(String person:data.keySet()) {
	        PersonCounts personData = data.get(person);
	        String category = personData.getCategory(algorithm);
	        personCategoryMap.put(person, category);
	    }
	    return personCategoryMap;
	}
	
	public Map<String, PersonCounts> getPersonData() {
	    return (Map<String, PersonCounts>) Collections.unmodifiableMap(data);
	}
	
	/*
	public String getCategory(String person) {
	    if(person == null) {
	        return null;
	    }
	    HashMap<String, Integer> personData = data.get(person);
	    if(personData == null) {
	        return null;
	    }
	    
	    //Algorithm
	    Integer CKD_Count = personData.get("CKD");
	    Integer HF_Count = personData.get("HF");
        Integer CAD_Count = personData.get("CAD");
        Integer DM_Count = personData.get("DM");
        Integer COPD_Count = personData.get("COPD");
        Integer DEP_Count = personData.get("DEP");
        Integer ASTH_Count = personData.get("ASTH");
        Integer HTN_Count = personData.get("HTN");
        Integer LD_Count = personData.get("LD");
	    
        if(CKD_Count == null) {
	        CKD_Count = 0;
	    }
	    if(HF_Count == null) {
	        HF_Count = 0;
	    }
	    if(CAD_Count == null) {
	        CAD_Count = 0;
	    }
	    if(DM_Count == null) {
	        DM_Count = 0;
	    }
	    if(COPD_Count == null) {
	        COPD_Count = 0;
	    }
	    if(DEP_Count == null) {
	        DEP_Count = 0;
	    }
	    if(ASTH_Count == null) {
	        ASTH_Count = 0;
	    }
	    if(HTN_Count == null) {
	        HTN_Count = 0;
	    }
	    if(LD_Count == null) {
	        LD_Count = 0;
	    }
	    	    
	    String category;
	    
	    if(CKD_Count >= 1) {
	        category = "CKD";
	    }
	    else if (HF_Count >=1 && CAD_Count >= 1 && DM_Count >= 1) {
	        category = "HF & CAD & DM";
	    }
	    else if (HF_Count >=1 && CAD_Count >= 1) {
	        category = "HF & CAD";
	    }
	    else if (HF_Count >=1 && DM_Count >= 1) {
	        category = "HF & DM";
	    }
	    else if (HF_Count >=1) {
	        category = "HF";
	    }
	    else if (CAD_Count >= 1 && DM_Count >= 1) {
	        category = "CAD & DM";
	    }
	    else if (CAD_Count >= 1) {
	        category = "CAD";
	    }
	    else if (DM_Count >= 1) {
	        category = "DM";
	    }
	    else if (COPD_Count >= 1) {
	        category = "COPD";
	    }
	    else if (DEP_Count >= 1) {
	        category = "DEP";
	    }
	    else if (ASTH_Count >= 1) {
	        category = "ASTH";
	    }
	    else if (HTN_Count >= 2 || LD_Count >= 2) {
	        category = "CV Risk";
	    }
	    else if (HTN_Count == 1 || LD_Count == 1) {
	        category = "At Risk";
	    }
	    else {
	        category = "Healthy";
	    }
	    
	    return category;
	} */
	
}
